package co.edu.usbcali.presentation.backingBeans;

import co.edu.usbcali.exceptions.*;
import co.edu.usbcali.modelo.*;
import co.edu.usbcali.modelo.dto.TipoEventosDTO;
import co.edu.usbcali.presentation.businessDelegate.*;
import co.edu.usbcali.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.RowEditEvent;

import java.io.Serializable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
@ManagedBean
@ViewScoped
public class TipoEventosView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtNombret;
    private InputText txtIdTe;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoEventosDTO> data;
    private TipoEventosDTO selectedTipoEventos;
    private TipoEventos entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public TipoEventosView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoEventosDTO tipoEventosDTO = (TipoEventosDTO) e.getObject();

            if (txtNombret == null) {
                txtNombret = new InputText();
            }

            txtNombret.setValue(tipoEventosDTO.getNombret());

            if (txtIdTe == null) {
                txtIdTe = new InputText();
            }

            txtIdTe.setValue(tipoEventosDTO.getIdTe());

            Long idTe = FacesUtils.checkLong(txtIdTe);
            entity = businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+""));

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedTipoEventos = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedTipoEventos = null;

        if (txtNombret != null) {
            txtNombret.setValue(null);
            txtNombret.setDisabled(true);
        }

        if (txtIdTe != null) {
            txtIdTe.setValue(null);
            txtIdTe.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() {
        try {
            Long idTe = FacesUtils.checkLong(txtIdTe);
            entity = (idTe != null)
                ? businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+"")) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtNombret.setDisabled(false);
            txtIdTe.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtNombret.setValue(entity.getNombre());
            txtNombret.setDisabled(false);
            txtIdTe.setValue(entity.getIdTipoevento());
            txtIdTe.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedTipoEventos = (TipoEventosDTO) (evt.getComponent()
                                                   .getAttributes()
                                                   .get("selectedTipoEventos"));
        txtNombret.setValue(selectedTipoEventos.getNombret());
        txtNombret.setDisabled(false);
        txtIdTe.setValue(selectedTipoEventos.getIdTe());
        txtIdTe.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedTipoEventos == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new TipoEventos();

            Integer idTe = FacesUtils.checkInteger(txtIdTe);

            entity.setIdTipoevento(idTe);
            entity.setNombre(FacesUtils.checkString(txtNombret));
            businessDelegatorView.saveTipoEventos(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idTe = new Long(selectedTipoEventos.getIdTe());
                entity = businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+""));
            }

            entity.setNombre(FacesUtils.checkString(txtNombret));
            businessDelegatorView.updateTipoEventos(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedTipoEventos = (TipoEventosDTO) (evt.getComponent()
                                                       .getAttributes()
                                                       .get("selectedTipoEventos"));

            Long idTe = new Long(selectedTipoEventos.getIdTe());
            entity = businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+""));
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idTe = FacesUtils.checkLong(txtIdTe);
            entity = businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+""));
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteTipoEventos(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedTipoEventos = (TipoEventosDTO) (evt.getComponent()
                                                       .getAttributes()
                                                       .get("selectedTipoEventos"));

            Long idTe = new Long(selectedTipoEventos.getIdTe());
            entity = businessDelegatorView.getTipoEventos(Integer.parseInt(idTe+""));
            businessDelegatorView.deleteTipoEventos(entity);
            data.remove(selectedTipoEventos);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long idTe, String nombret)
        throws Exception {
        try {
            entity.setNombre(FacesUtils.checkString(nombret));
            businessDelegatorView.updateTipoEventos(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoEventosView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtNombret() {
        return txtNombret;
    }

    public void setTxtNombret(InputText txtNombret) {
        this.txtNombret = txtNombret;
    }

    public InputText getTxtIdTe() {
        return txtIdTe;
    }

    public void setTxtIdTe(InputText txtIdTe) {
        this.txtIdTe = txtIdTe;
    }

    public List<TipoEventosDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataTipoEventos();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoEventosDTO> tipoEventosDTO) {
        this.data = tipoEventosDTO;
    }

    public TipoEventosDTO getSelectedTipoEventos() {
        return selectedTipoEventos;
    }

    public void setSelectedTipoEventos(TipoEventosDTO tipoEventos) {
        this.selectedTipoEventos = tipoEventos;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
